package restaurant;

/**
 * The Command object to rate a menu item.
 * 
 * @author Odessa
 */

public class cmdRateItem implements Command
{
	/** The information aggregator. */
	private InformationAggregator info;

	/** The item number of the item to rate. */
	private int itemNo;

	/** The item's rating. */
	private int rating;

	/** The comment about the item. */
	private String comment;



	/**
	 * Instantiates a new rate item command.
	 * 
	 * @param info the information aggregator.
	 * @param itemNo the item number to rate.
	 * @param rating the rating.
	 * @param comment the comment.
	 */
	public cmdRateItem(InformationAggregator info, int itemNo, int rating,
			String comment) {
		this.info = info;
		this.itemNo = itemNo;
		this.rating = rating;
		this.comment = comment;
	}



	/**
	 * Creates and adds a new item rating. Once the rating is added, the average
	 * rating for that menu item is calculated and then set.
	 * 
	 * @return the newly created item rating.
	 * @see restaurant.Command#execute()
	 */
	@Override
	public Object execute()
	{
		CustomerComments comments = info.getComments();
		Comment newComment = comments.addRating(itemNo, rating, comment);

		float average = comments.calcAvgRating(itemNo);

		Menu menu = info.getMenu();
		menu.setItemRating(itemNo, average);

		return newComment;
	}

}
